/*******************************************************************************
 * Copyright (C) 2018 Mestrelab Research S.L. All rights reserved.
 *
 * This file is part of the MNova scripting toolkit.
 *
 * Authorized users of MNova Software may use this file freely, but this file is 
 * provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * ****************************************************************************/

/*jslint plusplus: true, nomen: true, indent: 4, white: true*/
/*global qsTr, ByteArray, BinaryStream, File, print*/


function serializeTest() {
	'use strict';

	var obj = {
		num: 12,
		floatNum: 3.14159,
		str: "Custom String",
		utf8Str: "Николай",
		"Николай": "Utf8Key",
		arr: [1, "2", 3],
		boolParam: true,
		nullObj: null,
		undefObj: undefined,
		internalObj: { key: "Custom Value", key2: "Another Key"},
		customFunction: function () { print("Hello"); }, //should not be serialized
		qObj: new ByteArray("Some Text") //only properties should be serialized
	}, strm, bArr = new ByteArray(), newObj;
	
	//uncommenting this should throw an recursive storing error
	//obj.arr.push(obj);

	strm = new BinaryStream(bArr);
	strm.writeObj(obj);
	print("ByteArray size: {0}".format(bArr.size));

	strm.pos = 0;
	newObj = strm.readObj();

	print("Read object:");
	print(JSON.stringify(newObj, null, " "));
}

function CustomCtor() {
	'use strict';

	this.num = 0;
	this.floatNum = 0.0;
}

CustomCtor.prototype.constructorName = "CustomCtor";

CustomCtor.prototype.storeObj = function (aStr) {
	'use strict';
	aStr.writeString(JSON.stringify(this));
};

CustomCtor.prototype.loadObj = function (aStr) {
	'use strict';

	var str = aStr.readString(),
		loaded = JSON.parse(str);

	Object.keys(loaded).forEach(function (aK) {
		this[aK] = loaded[aK];
	}, this);
};

function customSerializeTest() {
	'use strict';

	var obj = new CustomCtor(),
		bArr = new ByteArray(), strm = new BinaryStream(bArr), newObj;

	obj.num = 3;
	obj.floatNum = 3.14159;
	
	strm.writeObj(obj);

	print("ByteArray size: {0}".format(bArr.size));

	strm.pos = 0;
	newObj = strm.readObj();

	print("Read object:");
	print(JSON.stringify(newObj, null, " "));
}

